package com.zx.callback.common.utils;

import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 * 文件下载
 * Created by zhaox on 17/07/25.
 */
public class DownloadFileUtil {

    /**
     * 文件下载工具类
     * @param filePath 文件路径（包括文件名）
     * @param fileName 下载后展示的文件文
     * @param response 输出
     * @throws IOException
     */
    public static void download(String filePath,String fileName, HttpServletResponse response) throws IOException{
        File file = new File(filePath);
        InputStream inputStream = new BufferedInputStream(new FileInputStream(file));
        byte[] buffer = new byte[inputStream.available()];
        inputStream.read(buffer);
        inputStream.close();
        response.reset();
        response.addHeader("Content-Disposition", "attachment;filename=" + new String(fileName.replaceAll(" ", "").getBytes("utf-8"),"iso8859-1"));
        response.addHeader("Content-Length",""+file.length());
        OutputStream os = new BufferedOutputStream(response.getOutputStream());
        os.write(buffer);
        os.flush();
        os.close();
    }
}
